/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * TelaErro.java
 *
 * Created on 30/03/2013, 13:27:48
 */
package com.bs.util;

import com.bs.SplashViewFrame;
import java.util.Properties;
import org.hibernate.exception.ConstraintViolationException;
import org.hibernate.exception.GenericJDBCException;

/**
 *
 * @author Bone
 */
public class TelaErro extends javax.swing.JDialog {

    private boolean finalizarAtivado = true;
    private boolean fecharAtivado = true;
    private static Properties mensagens = SplashViewFrame.getMensagens();

    public TelaErro(Exception ex) {
        initComponents();

        if (ex instanceof ConstraintViolationException) {
            ConstraintViolationException g =  (ConstraintViolationException) ex;
            ex = extraiExcessao(ex);
            extraiMensagem(g.getErrorCode(), g.getSQLState(), ex.getMessage());
        }else if (ex instanceof GenericJDBCException) {
            GenericJDBCException g = (GenericJDBCException) ex;
            ex = extraiExcessao(ex);
            extraiMensagem(g.getErrorCode(), g.getSQLState(), ex.getMessage());
        }else if (ex instanceof org.hibernate.exception.DataException) {
            org.hibernate.exception.DataException g = (org.hibernate.exception.DataException) ex;
            ex = extraiExcessao(ex);
            extraiMensagem(g.getErrorCode(), g.getSQLState(), ex.getMessage());
        }else if (ex instanceof org.hibernate.exception.GenericJDBCException) {
            org.hibernate.exception.GenericJDBCException g = (org.hibernate.exception.GenericJDBCException) ex;
            ex = extraiExcessao(ex);
            extraiMensagem(g.getErrorCode(), g.getSQLState(), ex.getMessage());
        }else if (ex instanceof org.hibernate.exception.JDBCConnectionException) {
            org.hibernate.exception.GenericJDBCException g = (org.hibernate.exception.GenericJDBCException) ex;
            ex = extraiExcessao(ex);
            extraiMensagem(g.getErrorCode(), g.getSQLState(), ex.getMessage());
        } else {
            ex = extraiExcessao(ex);
            erroLabel.setText("Erro: " + ex.getMessage());
            System.out.println(ex.getMessage());
        }
    }
    private void extraiMensagem(int errorCode, String sqlState,String mensagemOriginal){
        if (mensagens == null) {
                erroLabel.setText("Erro: " + errorCode + " : " + sqlState);
            } else {
                String msg = mensagens.getProperty(String.valueOf(errorCode));
                if (msg.equals("${system}")) {
                    erroLabel.setText("Erro: " + errorCode + " : " + mensagemOriginal);
                } else {
                    erroLabel.setText("Erro: " + errorCode + " : " + msg);
                }
            }
    }
    private Exception extraiExcessao(Exception ex) {
        while (ex.getCause() != null) {
            ex = (Exception) ex.getCause();
        }
        return ex;
    }

    public static void exibeErro(Exception ex) {
        TelaErro t = new TelaErro(ex);
        t.setVisible(true);
        t = null;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        jPanel1 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        erroLabel = new javax.swing.JLabel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setModal(true);
        setUndecorated(true);

        jPanel1.setBorder(new javax.swing.border.SoftBevelBorder(javax.swing.border.BevelBorder.RAISED));

        jButton1.setText("Fechar janela");

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${fecharAtivado}"), jButton1, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);

        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        erroLabel.setFont(new java.awt.Font("Tahoma", 0, 14)); // NOI18N
        erroLabel.setText("Mensagem");

        jButton2.setText("Finalizar aplicação");

        binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, this, org.jdesktop.beansbinding.ELProperty.create("${finalizarAtivado}"), jButton2, org.jdesktop.beansbinding.BeanProperty.create("enabled"));
        binding.setSourceUnreadableValue(null);
        bindingGroup.addBinding(binding);

        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                        .addGap(0, 390, Short.MAX_VALUE)
                        .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(erroLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(erroLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 33, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 45, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        bindingGroup.bind();

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jButton2ActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel erroLabel;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JPanel jPanel1;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables

    public boolean isFecharAtivado() {
        return fecharAtivado;
    }

    public void setFecharAtivado(boolean fecharAtivado) {
        boolean old = fecharAtivado;
        this.fecharAtivado = fecharAtivado;
        firePropertyChange("fecharAtivado", old, fecharAtivado);
    }

    public boolean isFinalizarAtivado() {
        return finalizarAtivado;
    }

    public void setFinalizarAtivado(boolean finalizarAtivado) {
        boolean old = this.finalizarAtivado;
        this.finalizarAtivado = finalizarAtivado;
        firePropertyChange("finalizarAtivado", old, finalizarAtivado);
    }
}
